import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { InsertResult, Repository } from 'typeorm';
import { UserEntity } from './User.entity';
import { log } from 'winston';

@Injectable()
export class UserService {

  constructor(
    @InjectRepository(UserEntity)
    private readonly userRepository: Repository<UserEntity>,
  ) {
  }

  async findOne(userId: string): Promise<UserEntity> {
    const user = await this.userRepository.findOne({
      where: {
        id: userId,
      },
    });
    if (!user) {
      throw new Error('user not found');
    }
    return user;
  }


  async findAll(): Promise<UserEntity[]> {
    return await this.userRepository.find();
  }


  async addUser(user: UserEntity) {
    try {
      await this.userRepository.insert(user);
      return "插入成功"
    } catch (e) {
      throw new Error(e);
    }
  }
}
